package com.dragon.backstage.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Paths;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.customizers.OpenApiCustomizer;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Swagger配置
 *
 * @author dragon
 * @version 1.0
 * @date 2024/7/29 15:24
 */
@Configuration
public class SwaggerConfig {


    @Bean
    public OpenAPI openApi() {
        return new OpenAPI()
            .info(
                new Info()
                    // 标题
                    .title("dragonBackstage接口文档")
                    // 描述
                    .description("dragonBackstage接口文档描述")
                    // 版本号
                    .version("1.0.0")
                    // 作者信息
                    .contact(
                        new Contact()
                            .name("dragon")
                            .url("https://www.dragon.com")
                            .email("dragon@qq.com")
                    )
                    // 许可证
                    .license(
                        new License()
                            .name("Apache 2.0")
                            .url("https://www.apache.org/licenses/LICENSE-2.0")
                    )
            )
            .externalDocs(
                new ExternalDocumentation()
                    .description("springdoc-openapi文档")
                    .url("https://springdoc.org/index.html")
            )
            .components(
                new Components()
                    .addSecuritySchemes("bearer-key", new SecurityScheme()
                        .name("bearer-key")
                        .type(SecurityScheme.Type.HTTP)
                        .scheme("bearer")
                        .bearerFormat("JWT")
                        .in(SecurityScheme.In.HEADER)
                    )
                    .addSecuritySchemes("mySecretHeader", new SecurityScheme()
                        .type(SecurityScheme.Type.APIKEY)
                        .in(SecurityScheme.In.HEADER)
                        .name("missingParam")
                    )
            )
            ;
    }

    // @Bean
    public GroupedOpenApi backstageApi() {
        return GroupedOpenApi.builder()
            .group("后台管理")
            .pathsToMatch("/**")
            .addOpenApiCustomizer(openApi -> {
                Paths paths = openApi.getPaths();
                paths.values().forEach(pathItem -> {
                    pathItem.readOperations().forEach(operation -> {
                        operation.addParametersItem(
                            new Parameter()
                                .in("header")
                                .name("X-Token")
                                .description("令牌")
                                .schema(new Schema<>().type("string"))
                        );
                    });
                });
            })
            .build();
    }

    @Bean
    public OpenApiCustomizer openApiCustomizer() {
        return openApi -> {
            // 全局添加请求头
            openApi.getPaths().values().forEach(pathItem -> {
                pathItem.readOperations().forEach(operation -> {
                    operation.addParametersItem(
                        new Parameter()
                            .in("header")
                            .name("X-Token")
                            .description("令牌")
                            .schema(new Schema<>().type("string"))
                    );
                });
            });
        };
    }

}
